<template>
  <div>
    <div class="firstLoading" v-if="!bookDetail">
      <van-loading
        size="30px"
        text-size="30px"
        text-color="#adadad"
      />Loding....
    </div>
    <div class="catalogue" id="scrollMain" ref="scrollMain">
      <van-sticky :offset-top="0">
        <div class="top_nav">
          <div class="nav">
            <span class="iconfont icon-youjiantou1" @click="back"></span>
            <i>目录</i>
          </div>
        </div>
      </van-sticky>
      <div class="book_info">
        <div class="left">
          <div class="img">
            <img :src="bookDetail.coverImage" alt="" />
          </div>
        </div>
        <div class="right">
          <h3>{{ bookDetail.title }}</h3>
          <p>{{ bookDetail.author }}</p>
        </div>
      </div>
      <div class="order">
        <span>共{{ bookCatalogue.length }}章</span>
        <div class="order_btn">
          倒序<span class="iconfont icon-xiajiantou"></span>
        </div>
      </div>

      <div class="small_title">正文</div>
      <div class="list" ref="list">
        <ul>
          <router-link
            tag="li"
            :to="{
              name: 'novel',
              query: { sectionId: catalog.secId, id: bookDetail.sourceUuid },
            }"
            v-for="catalog in bookCatalogue"
            :key="catalog.secId"
            @click.native="updateBookInfo(catalog.secId, bookDetail.sourceUuid)"
          >
            {{ catalog.title }}
          </router-link>
        </ul>
        <p class="moreLoading" v-if="loadingShow">lodingMore...</p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: ["bookCover", "bookDetail", "allBookList", "bookInfo"],
  data() {
    return {
      listsIndex: 0,
      loadingShow: false,
      bookCatalogue: [],
    };
  },
  created() {
    this.bookCatalogue = this.allBookList.slice(0, 100);
  },
  mounted() {
    this.$nextTick(() => {
      //触底不触发,没有添加到scroll事件
      //数据到dom渲染很慢使用nexttick也无法先获取到ref
      window.addEventListener("scroll", this.scrollGet, true);
    });
  },

  methods: {
    updateBookInfo(catalogId, bookId) {
      let objLoc = {
        title: this.bookDetail.title,
        catalogId: catalogId,
        bookId: bookId,
        catalog: this.allBookList,
        coverImage: this.bookDetail.coverImage,
        author: this.bookDetail.author,
      };
      this.$emit("update-bookInfo", objLoc);
    },

    scrollGet() {
      let scrollTop =
        document.body.scrollTop || document.documentElement.scrollTop || 0;
      //全部内容高度
      let scrollHeight = document.documentElement.scrollHeight;
      //当前内容的可视高度
      let clientHeight = document.documentElement.clientHeight;
      if (Math.round(scrollTop + clientHeight) >= scrollHeight) {
        console.log(11111);
        this.loadingShow = true;
        setTimeout(() => {
          this.morePull();
          this.loadingShow = false;
        }, 2000);
      }
      console.log(scrollTop,clientHeight,scrollHeight);
    },
    morePull() {
      console.log(111);
      ++this.listsIndex;
      let data = this.allBookList.slice(
        100 * this.listsIndex,
        this.listsIndex * 100 + 100
      );
      console.log(data);
      // this.bookCatalogue.push(...data);
      data.forEach((element) => {
        if (element) {
          this.bookCatalogue.push(element);
          // this.$emit("slice-catalog",)
        }
      });
    },
    back() {
      this.$emit("change-show-catalogue", false);
    },
  },
  beforeDestroy() {
    window.removeEventListener("scroll", this.scrollGet);
  },
};
</script>


<style lang="less" scoped>
.firstLoading {
  width: 100vw;
  height: 100vh;
  z-index: 1;
  position: absolute;
  top: 0;
  left: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}
.catalogue {
  position: absolute;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100%;
  background: #f5f5f5;
  background: #fff;

  // overflow-y: scroll;
  .top_nav {
    background-color: #fff;
    padding: 0 13px;
    .nav {
      display: flex;
      align-items: center;
      height: 35px;
      span {
        color: #706e6b;
        font-size: 15px;
        font-weight: 600;
      }
      i {
        font-size: 18px;
        flex: 1;
        display: block;
        width: 100%;
        text-align: center;

        font-style: none;
      }
    }
  }

  .book_info {
    display: flex;
    align-items: center;
    padding: 20px 13px;
    .left {
      .img {
        width: 47px;
        height: 60px;
        box-shadow: 3px 2px 8px -3px #706e6b;
        img {
          width: 47px;
          height: 60px;
        }
      }
    }
    .right {
      margin-left: 10px;
      .h3 {
        font-size: 15px;
        color: #606060;
      }
      p {
        margin-top: 10px;
        font-size: 12px;
        color: #adadad;
      }
    }
  }
  .order {
    margin-top: 10px;
    padding: 0 13px;
    display: flex;
    justify-content: space-between;
    span {
      color: #2b2b2b;
      font-size: 12px;
    }
    div {
      font-size: 12px;
      color: #adadad;
      border: 1px solid #adadad;
      border-radius: 15px;
      padding: 0 3px;
      span {
        color: #adadad;
      }
    }
  }

  .small_title {
    padding: 0 13px;
    height: 28px;
    background: #f9f9f9;
    color: #cecece;
    font-size: 13px;
    line-height: 28px;
  }
  .list {
    background: #fff;
    padding: 0 13px;
    p.moreLoading {
      text-align: center;
      color: #adadad;
      height: 30px;
    }
    ul {
      padding: 0 13px;
      li {
        height: 35px;
        line-height: 35px;
        font-size: 13px;
        color: #b3b3b3;
        span {
          margin-left: 5px;
        }
      }
    }
  }
}
</style>